
<template>
  <div class="login-container">
    <div class="my-login-box">

      <div class="img-box">
        <img src="@/assets/logo.b5a855ee.png" alt="">
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />

        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <div class="btn-box">
          <el-button type="primary" @click.native.prevent="handleLogin">登录</el-button>

          <el-button class="btn-2">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const chickUsername = (rule, value, callback) => {
      const arr = /^[a-z]+$/i
      if (!arr.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入手机号' }, { validator: chickUsername }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录按钮
    handleLogin() {
      this.$refs.loginForm.validate(async(flag) => {
        // console.log(flag)// validate()对整个表单进行验证的方法
        if (!flag) {
          return
        } else {
          await this.$store.dispatch('user/login', this.loginForm)
          this.$router.push('/')

        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #606266;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;

  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      // border: 0px;
       width: 410px;
      height: 40px;

      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 35px;
      // color: $light_gray;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100%;
  width: 100%;
  background-color: #2b4b6b;
  overflow: hidden;

// 登录盒子
.my-login-box{
  position: relative;
  width: 450px;
  height: 300px;
  background-color:#fff;
  // logo模块
  .img-box{
    position: absolute;
    top: -75px;
    left: 150px;
    width: 150px;
    height: 150px;
    padding: 20px;
    // box-shadow: 0 0 10px #ddd;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    box-sizing: border-box;
    img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
    }

  }
  .el-input__inner{
    // color: red !important;
  }

}
  .login-form {
    position: absolute;
    bottom: 0;
    max-width: 100%;
    overflow: hidden;
    width: 450px;
    height: 187px;
    padding: 0 20px;
   .btn-box{
    width: 100%;
    margin-bottom: 22px;
    display: flex;
    justify-content: flex-end;
    .btn-2{
    color: #FFF;
    background-color: #909399;
    border-color: #909399;
    }
  }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login-container .el-input input{
    color: red !important;

  }
    .el-form-item__content{
    .svg-container{
     position: absolute;
     top: -6px;
}
  }
}
</style>
